/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8                               :vi │
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2025 Justine Alexandra Roberts Tunney                              │
│                                                                              │
│ Permission to use, copy, modify, and/or distribute this software for         │
│ any purpose with or without fee is hereby granted, provided that the         │
│ above copyright notice and this permission notice appear in all copies.      │
│                                                                              │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL                │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED                │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE             │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL         │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR        │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER               │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR             │
│ PERFORMANCE OF THIS SOFTWARE.                                                │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/fmt/leb128.h"
#include "libc/testlib/benchmark.h"
#include "libc/testlib/testlib.h"

TEST(unuleb64, test) {
  uint64_t x;
  EXPECT_EQ(1, unuleb64("\0", 1, &x));
  EXPECT_EQ(0, x);
  EXPECT_EQ(1, unuleb64("\1", 1, &x));
  EXPECT_EQ(1, x);
  EXPECT_EQ(1, unuleb64("\177", 1, &x));
  EXPECT_EQ(127, x);
  EXPECT_EQ(2, unuleb64("\377\0", 2, &x));
  EXPECT_EQ(127, x);
  EXPECT_EQ(2, unuleb64("\377\1", 2, &x));
  EXPECT_EQ(255, x);
  EXPECT_EQ(9, unuleb64("\377\377\377\377\377\377\377\377\0", 9, &x));
  EXPECT_EQ(0x00ffffffffffffff, x);
  EXPECT_EQ(9, unuleb64("\377\377\377\377\377\377\377\377\377\0", 10, &x));
  EXPECT_EQ(0xffffffffffffffff, x);
  EXPECT_EQ(9, unuleb64("\377\377\377\377\377\377\377\377\177\1", 10, &x));
  EXPECT_EQ(0x7fffffffffffffff, x);
  EXPECT_EQ(9, unuleb64("\377\377\377\377\377\377\377\377\177\1", 9, &x));
  EXPECT_EQ(0x7fffffffffffffff, x);
  EXPECT_EQ(-1, unuleb64("\377\377\377\377\377\377\377\377\377\2", 8, &x));
}

BENCH(unuleb64, bench) {
  uint64_t x;
  BENCHMARK(1000, 1, unuleb64("\4\0\0\0\0\0\0\0\0\0\0\0\0\0", 14, &x));
  BENCHMARK(1000, 10, unuleb64("\377\377\377\377\377\377\377\377\377", 9, &x));
}
